//
//  PopoverPresentationController.swift
//  CLWeiBo
//
//  Created by 程雷 on 15/6/28.
//  Copyright © 2015年 程雷. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {

    /// 展现视图的 frame
    var presentFrame = CGRectZero
    
    //遮罩视图
    lazy var dummyView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        
        let tap = UITapGestureRecognizer(target: self, action: "close")
        v.addGestureRecognizer(tap)
        return v

    }()
    
    /**
    containerView 容器视图，放置展现出来的视图的
    presentedView() 被展现出来的视图的
    */
    ///  实例化专场控制器
    ///
    ///  :param: presentedViewController  被展现的视图 description
    ///  :param: presentingViewController 文档上shuo，发起的控制器 Xcode 6 nil，Xcode 7 野指针

    ///
    ///  :returns: 展现控制器
    /*
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
        print(presentedViewController)
        //  presentingViewController 这里打印会报野指针错误
      //  print(presentingViewController)
        // 因为还没有显示，容器视图还没有创建
        print(containerView)
        prepareDummyView()
        
    }
    */
   //  let homeTab = HomeTableViewController()
    func close(){
        
        // 改变图片上下状态
        NSNotificationCenter.defaultCenter().postNotificationName(HomeTitleButtonClickNotification, object: nil)
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
//    private func prepareDummyView(){
//
//        let tap = UITapGestureRecognizer(target: self, action: "close")
//        dummyView.addGestureRecognizer(tap)
//    }
    
    //容器视图将要重新布局子视图
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        //添加并设置 dummyView
        dummyView.frame = containerView!.bounds
        containerView?.insertSubview(dummyView, atIndex:0)
        
        //设置子视图大小
        presentedView()?.frame = presentFrame
    }
}
